package com.scaffold.commons.json;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import wangjg.commons.bean.BeanUtil;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.scaffold.card.entity.Card;

/**
 * 
 * @author wangjg
 *
 */
public class JacksonUtil2 {
	public static Boolean beautifier = false;

	private ObjectMapper mapper = new ApiObjectMapper2();
	
	public JacksonUtil2() {
	}

	public ObjectMapper getMapper(){
		return this.mapper;
	}
	
	public String objToJson(Object obj){
		try {
			if(beautifier){
				return this.getMapper().writerWithDefaultPrettyPrinter().writeValueAsString(obj);
			}else{
				return this.getMapper().writeValueAsString(obj);
			}
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
	
	public Object jsonToObj(String jsonStr, Class<?> cls) {
		if (jsonStr == null || cls==null){
			return null;
		}
		try{
			return this.getMapper().readValue(jsonStr, cls);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
	
	public String objToPrettyJson(Object obj){
		try {
			String json = this.getMapper().writerWithDefaultPrettyPrinter().writeValueAsString(obj);
			return json;
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
	
	public <T> List<T> jsonToList(String jsonVal, Class<?> clazz) {
		TypeFactory t = TypeFactory.defaultInstance();
		try{
			List<T> list = this.getMapper().readValue(jsonVal,t.constructCollectionType(ArrayList.class, clazz));
			return list;
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
	
	public Map<?,?> jsonToMap(String jsonVal) {
		try{
			Map<?,?> map = this.getMapper().readValue(jsonVal, Map.class);
			return map;
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
	
	@SuppressWarnings("all")
	public List jsonToMapList(String jsonVal){
		try{
			List<LinkedHashMap<?, ?>> list = this.getMapper().readValue(jsonVal, List.class);
			return list;
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
	
	public Map<String,Object> o2o(Object bean){
		Map<String,Object> objMap = BeanUtil.getObjectMap(bean, true);
		
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("prop", objMap.keySet());
		map.put("val", objMap.values());
		
		return map;
	}
	
	public Map<String,Object> list2o(List<Object> list){
		HashSet<String> ppSet = new HashSet<String>();
		List<Map<String,Object>> maplList = new ArrayList<Map<String,Object>>();
		for(Object bean : list){
			Map<String,Object> objMap = BeanUtil.getObjectMap(bean, true);
			maplList.add(objMap);
			
			ppSet.addAll(objMap.keySet());
		}

		List<Object> valList = new ArrayList<Object>();
		for(Map<String,Object> objMap : maplList){
			List<Object> vList = new ArrayList<Object>();
			for(String pp : ppSet){
				Object val = objMap.get(pp);
				vList.add(val);
			}
			valList.add(vList);
		}
		
		Map<String,Object> map = new LinkedHashMap<String,Object>();
		map.put("prop", ppSet);
		map.put("val", valList);
		
		return map;
	}
	
	public Map<String,Object> list2o2(List<Object> list){
		HashSet<String> ppSet = new HashSet<String>();
		List<Map<String,Object>> maplList = new ArrayList<Map<String,Object>>();
		for(Object bean : list){
			Map<String,Object> objMap = BeanUtil.getObjectMap(bean, true);
			maplList.add(objMap);
			
			ppSet.addAll(objMap.keySet());
		}

		List<Object> valList = new ArrayList<Object>();
		for(Map<String,Object> objMap : maplList){
			for(String pp : ppSet){
				Object val = objMap.get(pp);
				if(val!=null){
					valList.add(val);	
				}else{
					valList.add(val);
				}
			}
		}
		
		Map<String,Object> map = new LinkedHashMap<String,Object>();
		map.put("prop", ppSet);
		map.put("val", valList);
		
		return map;
	}
	
	public static void main(String[] args) throws Exception {
		List<Object> list = new ArrayList<Object>();
		Card bean = new Card();
		bean.setId(1L);
		bean.setPicture("xxx");
		bean.setCategoryId(0L);
		list.add(bean);
		list.add(bean);
		
		Card bean3 = new Card();
		bean3.setId(1L);
		bean3.setVideo("d:\\a.mp4");
		list.add(bean3);

		JacksonUtil2 ju = new JacksonUtil2();
		System.out.println(list.size());
		System.out.println(ju.objToJson(list));
		System.out.println(ju.objToJson(ju.list2o(list)));
		System.out.println(ju.objToJson(ju.list2o2(list)));
		System.out.println(ju.objToJson("ab\"c,\t\n"));
		System.out.println(ju.jsonToMapList("[1,null,3]"));

	}
}

